<?php
/**
 * Created by PhpStorm.
 * User: zhangyongku
 * Date: 2018/6/13
 * Time: 15:02
 * Group: 视频信息接口
 */
namespace Video\Controllers\Api;

use Base\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;
use Video\Model\Comments;
use Video\Model\GiveLikes;
use Video\Model\Relation;
use Video\Model\Video;

class VideoController extends Controller
{
    /**
     * @api {post} /video/details/ 视频信息
     * @apiVersion 0.0.1
     * @apiName getVideoInfo
     * @apiGroup video
     * @apiDescription 视频信息
     * @apiParam {string} video_id 视频编号.
     * @apiParam {string} token  token.   //改token
     * @apiParam {string} video_uid.   //这条视频的用户ID
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "成功"
     *        "result":
     *          {
     *             "user_id":"1",
     *             "video_title" : "今天天气妹妹哒",
     *             "video_explain" : "今天天气妹妹哒",
     *             "video_qiniu_url" : "http://p9n2voq7j.bkt.clouddn.com/201806080639512399.mp4",
     *             "video_qiniu_img" : "http://p9n2voq7j.bkt.clouddn.com/201806080647222527.mp4?vframe/jpg/offset/2",
     *             "support_total" : "12"，
     *             "comments_total" : "2323"
     *             "dateline" : "23554545215"
     *             "status" : "1"
     *              "music_id" :    //音乐
     *              "share"  ： 2，   //分享次数
     *               "status": 1,               //视频状态0为待审核，1为已发布，2为删除', 默认都是已发布
     *                "dateline": 1528438090,   //发布时间
     *               "like": 0      //自己是否点赞    1=已点赞    ，0=未点赞
     *               "attention" :1          //是否已经关注此作者   1=已关注   0=未关注
     *              }
     *          }
     *
     *     }
     */
    public function getVideoInfo(Request $request){
        $xtoken=$request->header('x-auth-token');
        $videoId = $request->input('video_id');
        $video_uid = $request->input('video_uid');
        if(empty($xtoken)){
            return $this->response([],401,"Unauthorized");
        }
        if(empty($videoId)){
            return $this->response([],403,"缺少必要参数");
        }
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }

        $newInfo = array();
        $model = new Video();
        $giveModel = new GiveLikes();
        $comment = new Relation();
        $videoInfo = $model->where('video_id',$videoId)->get(['video_id','video_title','video_explain','video_qiniu_url','video_qiniu_img','video_qiniu_gif','support_total','comments_total','status','created_at']);

        if(!$videoInfo){
            return $this->response([],403,"该条信息不存在");
        }else{
            $videoArr = $videoInfo->toArray();

            $newInfo = array_merge($result['data'],$videoArr);
        }

        //视频是否点赞
        $data = $giveModel->where(['messageid'=>$videoId,'uid'=>$uid,'infotype'=>2])->first();
        if($data){
            $newInfo['like'] = 1;
        }else{
             $newInfo['like'] = 0;
        }
        //用户是否被关注
        $re = $comment->where(['uid'=>$uid,'rid'=>$video_uid])->first();
        if($re){
            $newInfo['attention'] = 1;
        }else{
            $newInfo['attention'] = 0;
        }
        return $this->response($newInfo,200,true);
    }

    /**
     * @api {post} /video/comment_show/ 视频评论内容
     * @apiVersion 0.0.1
     * @apiName comment_show
     * @apiGroup video
     * @apiDescription 视频评论内容
     * @apiParam {string} token  token.
     * @apiParam {number} video_id   视频ID
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "成功"
     *        "result":[
     *
     *      ]
     *
     *     }
     */
    public function commentShow(Request $request){
        $info = $request->all();
        $xtoken=$request->header('x-auth-token');
        if(empty($xtoken)){
            return $this->response([],401,"Unauthorized");
        }
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"身份验证错误");
        }
        if(empty($info['video_id'])){
            return $this->response("",403,"参数不全");
        }
        $model = new Comments();
        $data = $model->where('video_id',$info['video_id'])->get()->toArray();

        if(empty($data)){
          return $this->response([],500,"此信息无评论");
        }
        //评论者信息
        $ids = implode(',',array_column($data,'from_uid'));
        //评论 @ 的用户
        $to_ids = implode(',',array_column($data,'to_uid'));

        $member = post_curl_memberids($xtoken,$ids);
        if(!empty($to_ids)){
          $to_member = post_curl_memberids($xtoken,$to_ids);
        }
        if($member['code'] != 0){
          return $this->response([],500,"信息返回错误");
        }
        $memberInfo = $member['data'];
        $to_memberInfo = $to_member['data'];
        //评论的用户
        foreach ($data as $key => &$video) {
            foreach ($memberInfo as $k => $value) {
              // code...
              if($video['from_uid'] == $value['memberId']){
                unset($value['memberId']);
                foreach ($value as $key => $v) {
                  $video[$key] = $v;
                }
              }
            }
        }
        //评论@的用户
        if(!empty($to_memberInfo)){
            foreach ($data as $key => &$video) {
                foreach ($to_memberInfo as $k => $value) {
                  // code...
                  if($video['to_uid'] == $value['memberId']){
                    $video['to_user_nickname'] = $value['nickname'];
                  }
                }
            }
        }
        if($data){
            return $this->response($data,200,true);
        }
    }

    /**
     * @api {post} /video/recommend/ 推荐视频
     * @apiVersion 0.0.1
     * @apiName recommend
     * @apiGroup video
     * @apiDescription 推荐视频
     * @apiParam {string} token  token.
     * @apiParam {number} page   页码
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "成功"
     *        "result":[
     *
     *      ]
     *
     *     }
     */
    public function videoRecommend(Request $request){
            $info = $request->all();
            $xtoken=$request->header('x-auth-token');
            if(empty($xtoken)){
                return $this->response([],401,"Unauthorized");
            }
            $result = post_curl($xtoken);
            if($result == false){
                return $this->response([],500,"接口请求错误");
            }
            if($result['code'] != 0){
                return $this->response([],500,"信息返回错误");
            }

            $page = isset($info['page']) ? $info['page'] : 2;
            $start = ($page-1)*8;
            $model = new Video();
            $data  = $model->where(['status'=>1])
                        ->orderBy('support_total','desc')
                        ->limit($start,8)
                        ->get(['user_id','video_id','video_qiniu_url','support_total'])
                        ->toArray();
            if(empty($data)){
              return $this->response([],500,"无数据");
            }
             $ids = implode(',',array_column($data,'user_id'));
             $member = post_curl_memberids($xtoken,$ids);
             if($member['code'] != 0){
               return $this->response([],500,"信息返回错误");
             }
             $memberInfo = $member['data'];

             foreach ($data as $key => &$video) {
                 foreach ($memberInfo as $k => $value) {
                   if($video['user_id'] == $value['memberId']){
                   unset($value['memberId']);
                     foreach ($value as $u => $v) {
                       $video[$u] = $v;
                     }
                   }
                 }
             }
             return $this->response($data,200);
    }

    /**
     * @api {post} /video/givelike/ 点赞
     * @apiVersion 0.0.1
     * @apiName givelike
     * @apiGroup video
     * @apiDescription 点赞
     * @apiParam {number} messageid   被点赞信息的ID
     * @apiParam {number} infotype   被点赞信息的类型  点赞类型 2=视频，1=评论
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "点赞成功"
     *        "result":  true
     *     }
     */
    public function giveLike(Request $request){
        $info = $request->all();
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }
        if(empty($uid)){
            return $this->response([],401,"Unauthorized");
        }
        if(empty($info['messageid']) || empty($info['infotype'])){
            return $this->response([],403,"缺少必要参数");
        }
        $giveModel = new GiveLikes();
        $videoModel =new Video();
        $comment = new Comments();
        $data = $giveModel->where(['uid'=>$uid,'messageid'=>$info['messageid'],'infotype'=>$info['infotype']])->first();
        if($data){
            $re = $giveModel->where(['uid'=>$uid,'messageid'=>$info['messageid'],'infotype'=>$info['infotype']])->delete();
            return $this->response([],200,"取消点赞成功");
        }else{
            $re = $giveModel->insert(['uid'=>$uid,'messageid'=>$info['messageid'],'infotype'=>$info['infotype'],'created_at'=>time()]);
            if($info['infotype'] == 2){
                $videoLike = $videoModel->where('video_id',$info['messageid'])->first();
                if(!$videoLike){
                    return $this->response([],403,"此信息不存在");
                }
                $support_total = (int)$videoLike->support_total+1;
                $videoInfo = $videoModel->where('video_id',$info['messageid'])->update(['support_total'=>$support_total]);
                return $this->response([],200,"点赞成功");
            }else{
                $commentLike = $comment->where('comments_id',$info['messageid'])->first();
                if(!$commentLike){
                    return $this->response([],403,"此信息不存在");
                }
                $praise_num = (int)$commentLike->praise_num+1;
                $comment = DB::table('video_comments')->where('comments_id',$info['messageid'])->update(['praise_num'=>$praise_num]);
                return $this->response([],200,"点赞成功");
            }

        }


    }


    /**
     * @api {post} /video/comment/ 视频 or 评论/评论
     * @apiVersion 0.0.1
     * @apiName comment
     * @apiGroup video
     * @apiDescription 视频/评论/评论
     * @apiParam {string} token  token.
     * @apiParam {string} video_id  视频编号.
     * @apiParam {string} comments_content  评论内容.
     * @apiParam {string} to_uid   评论目标ID，如果没有则是针对视频的评论
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "评论成功"
     *        "result":  true
     *     }
     */

    public function  comment(Request $request){
        $info = $request->all();
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }
        if(empty($uid)){
            return $this->response([],401,"Unauthorized");
        }
        if(empty($info['video_id']) || empty($info['comments_content'])){
            return $this->response([],403,"参数不完整");
        }
        $arr = array(
            'video_id' => $info['video_id'],
            'from_uid' => $uid,
            'comments_content' => $info['comments_content'],
            'to_uid' => isset($info['to_uid']) ? $info['to_uid'] : '',
        );
        $model = new Comments();
        $video = new Video();
        $data = $model->insert($arr);
        if($data){
            $video = $video->where('video_id',$info['video_id'])->first();
            $comments_total = (int)$video->comments_total+1;
            $videoInfo = $video->where('video_id',$info['video_id'])->update(['comments_total'=>$comments_total]);
            return $this->response([],200,"评论成功");
        }

    }


    /**
     * @api {post} /video/upload_video / 发布上传视频
     * @apiVersion 0.0.1
     * @apiName upload_video
     * @apiGroup video
     * @apiDescription 发布上传视频
     * @apiParam {string} x-auth-token
     * @apiParam {file} video_file  视频文件.
     * @apiParam {string} address  位置
     * @apiParam {string} content  说明描述
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "上传成功"
     *        "result":  true
     *     }
     */

    public function  uploadVideo(Request $request){
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }
        if(empty($uid)){
            return $this->response([],401,"Unauthorized");
        }
        $input = $request->all();
        if(!empty($input['address'])){
          $address = adderss_lng_lat($input['address']);
          $longitude = $address[0];
          $latitude = $address[1];
        }else{
          $longitude = '';
          $latitude = '';
        }


        $file = $request->file('video_file');
        $video_extensions = ["wmv", "mov", "asf","asx","3gp","mp4","avi","mp3"];
        if($file->isValid()) {
            if ($file->getClientOriginalExtension() && in_array($file->getClientOriginalExtension(), $video_extensions)) {

                $accessKey = env('QINIU_ACCESS_KEY');
                $secretKey = env('QINIU_SECRET_KEY');
                $bucket = env('QINIU_BUCKET');
                // 构建鉴权对象

                $auth = new Auth($accessKey, $secretKey);
                $key = $uid .'/'.date("YmdHis", time()) . rand(1000, 9999) . '.' . "mp4";
                if($file->getClientOriginalExtension() != 'mp4') {
                    //$wmImg = Qiniu\base64_urlSafeEncode('http://rwxf.qiniudn.com/logo-s.png');    //视频水印图片
                        //$pfopOps = "avthumb/mp4/s/640x360/vb/1.4m/wmImage/$wmImg|saveas/".\Qiniu\base64_urlSafeEncode(config("video.qiniu.QINIU_BUCKET") . ":".$key);
                    $pfopOps = "avthumb/mp4/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":" . $key);
                    // $pfopOpsGif = "vsample/png/ss/1/t/3/s/480x360/pattern/".\Qiniu\base64_urlSafeEncode(config("video.qiniu.QINIU_BUCKET") . ":" . $keyGif);
                    $policy = array(
                        'persistentOps' => $pfopOps,
                        'deadline' => 1390528576,
                        'persistentNotifyUrl' => 'http://alliance.test.com/video/add',
                    );

                    // 生成上传 Token
                    $token = $auth->uploadToken($bucket, null, 3600, $policy);
                }else{
                    $token = $auth->uploadToken($bucket);
                }
                // 要上传文件的本地路径
                $filePath = $file->getRealPath();
                // 初始化 UploadManager 对象并进行文件的上传。
                $uploadMgr = new UploadManager();
                // 调用 UploadManager 的 putFile 方法进行文件的上传。
                list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
                if ($err !== null) {
                  return $this->response([],500,$err);
                } else {
                    //上传成功  整理数据
                    $arr = array(
                        'user_id' => $uid,
                        'video_explain' => $input['content'],
                        'video_qiniu_url' => env('QINIU_DOMAIN') . '/' . $ret['key'],
                        'video_qiniu_img' => env('QINIU_DOMAIN') . '/' . $ret['key']."?vframe/jpg/offset/2",    //此处可以增加变量设置生成图片的大小和帧数
                        // 'video_qiniu_gif' => config('video.qiniu.QINIU_DOMAIN') . '/' . $ret['key']."vsample/gif/ss/2/t/4/s/480x360/pattern/$url_safe_base64",    //
                        'video_qiniu_hash' => $ret['hash'],
                        'video_size' => sizecount($file->getSize()),
                        'video_mimetye' => $file->getMimeType(),
                        'persistentId' => isset($ret['persistentId']) ? $ret['persistentId'] : '',
                        'status' => 1,
                        'created_at' => time(),
                        'longitude' => $longitude,
                        'latitude' => $latitude,
                    );
                    $model = new Video();
                    $data = $model->insert($arr);
                    if ($data) {
                       return $this->response([],200,"上传成功");
                    }

                }

            }else{
                return $this->response([],403,"文件类型错误");
            }
        }else{
          return $this->response([],403,"请上传视频");
        }
    }

    /**
     * @api {post} /video/my_works / 我的作品列表
     * @apiVersion 0.0.1
     * @apiName my_works
     * @apiGroup video
     * @apiDescription 我的作品列表
     * @apiParam {string} page  页码.  页码从2开始
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": ""
     *        "result":  true
     *     }
     */

    public function myWorks(Request $request){
        $info = $request->all();
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }
        if(empty($uid)){
            return $this->response([],401,"Unauthorized");
        }
        $page = isset($info['page']) ? $info['page'] : 2;
        $start = ($page-1)*8;
        $model = new Video();
        $data =$model->where(array('user_id'=>$uid,'status'=>1))
                    ->orderBy('created_at','desc')
                    ->limit($start,8)
                    ->get(['user_id','video_id','video_qiniu_gif','support_total','share'])
                    ->toArray();
        if(empty($data)){
          return $this->response([],500,"无数据");
        }
        $ids = implode(',',array_column($data,'user_id'));
        $member = post_curl_memberids($xtoken,$ids);
        if($member['code'] != 0){
          return $this->response([],500,"用户服务器错误");
        }
        $memberInfo = $member['data'];
        if(empty($memberInfo)){
          return $this->response([],500,"该用户不存在");
        }
        foreach ($data as $key => &$video) {
            foreach ($memberInfo as $k => $value) {
              if($video['user_id'] == $value['memberId']){
              unset($value['memberId']);
                foreach ($value as $u => $v) {
                  $video[$u] = $v;
                }
              }
            }
        }
        if($data){
            return $this->response($data,200);
        }else{
            return $this->response([],404,"not found");
        }
    }

    /**
     * @api {post} /video/my_like / 我喜欢的列表
     * @apiVersion 0.0.1
     * @apiName my_like
     * @apiGroup video
     * @apiDescription 我喜欢的列表
     * @apiParam {string} page  页码.  页码从2开始
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": ""
     *        "result":  true
     *     }
     */

    public function  myLike(Request $request){
        $info = $request->all();
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }
        if(empty($uid)){
            return $this->response([],401,"Unauthorized");
        }
        $page = isset($info['page']) ? $info['page'] : 2;
        $start = ($page-1)*8;
        $model = new GiveLikes();
        $data = $model
                ->leftjoin('video_info','video_info.video_id','=','messageid')
                ->where(['infotype'=>2,'uid'=>$uid])
                ->orderBy('created_at','desc')
                ->limit($start,8)
                ->get(['video_info.*'])->toArray();
        if(empty($data)){
          return $this->response([],500,"无数据");
        }
        $ids = implode(',',array_column($data,'user_id'));
        $member = post_curl_memberids($xtoken,$ids);
        if($member['code'] != 0){
          return $this->response([],500,"用户服务器错误");
        }
        $memberInfo = $member['data'];
        if(empty($memberInfo)){
          return $this->response([],500,"用户信息验证失败");
        }
        foreach ($data as $key => &$video) {
            foreach ($memberInfo as $k => $value) {
              if($video['user_id'] == $value['memberId']){
              unset($value['memberId']);
                foreach ($value as $u => $v) {
                  $video[$u] = $v;
                }
              }
            }
        }
        if($data){

            return $this->response($data,200);
        }else{
            return $this->response([],404,"not found");
        }
    }

    /**
     * @api {post} /video/delete_works / 删除我的作品
     * @apiVersion 0.0.1
     * @apiName delete_works
     * @apiGroup video
     * @apiDescription 删除我的作品
     * @apiParam {string} video_id  视频Id.
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "删除成功"
     *        "result":  true
     *     }
     */

    public function deleteWorks(Request $request){
        $info = $request->all();
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $uid = $result['data']['memberId'];
        }
        if(empty($uid)){
            return $this->response([],401,"Unauthorized");
        }
        if(empty($info['video_id'])){
            return $this->response([],403,"缺少必要参数");
        }
        $model = new Video();
        $data = $model->where('video_id',$info['video_id'])->update(['status'=>2]);
        if($data){
            return $this->response([],204,"该视频删除成功");
        }else{
            return $this->response([],500,"未知错误");
        }
    }

    /**
     * @api {post} /video/attention / 关注
     * @apiVersion 0.0.1
     * @apiName attention
     * @apiGroup video
     * @apiDescription 关注
     * @apiParam {string} token  token.
     * @apiParam {string} uid  被关注用户ID
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "关注成功"
     *        "result":  true
     *     }
     */
    public function attention(Request $request){
        $uid = $request->input('uid');
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            $rid = $result['data']['memberId'];
        }
        if(empty($rid)){
            return $this->response([],401,"Unauthorized");
        }
        $model = new Relation();
        $info = $model->where(['uid'=>$uid,'rid'=>$rid])->first();
        if($info){
            $reslut = $model->where(['uid'=>$uid,'rid'=>$rid])->delete();
            return $this->response([],200,"取消关注成功");
        }else{
            $reslut = $model->insert(['uid'=>$uid,'rid'=>$rid]);
            return $this->response([],200,"关注成功");
        }

    }
    /**
     * @api {post} /video/share / 分享
     * @apiVersion 0.0.1
     * @apiName share
     * @apiGroup video
     * @apiDescription 分享
     * @apiParam {string} x-auth-token  token.
     * @apiParam {string} video_id  视频编号
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "分享成功"
     *        "result":  true
     *     }
     */

    public function share(Request $request){
        $videoId = $request->input('video_id');
        $xtoken=$request->header('x-auth-token');
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }
        if(empty($videoId)){
            return $this->response([],403,"参数不全");
        }
        $model = new Video();
        $data = $model->where('video_id',$videoId)->first();
        $share = (int)$data->share+1;
        $re = $model->where('video_id',$videoId)->update(['share'=>$share]);
        if($re){
            return $this->response([],200,"分享成功");
        }else{
            return $this->response([],500,"未知错误");
        }
    }

    /**
     * @api {post} /video/personal_info / 用户个人信息
     * @apiVersion 0.0.1
     * @apiName personal_info
     * @apiGroup video
     * @apiDescription 用户个人信息
     * @apiParam {string} x-auth-token  用户识别信息.
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": "关注成功"
     *        "result":  true
     *     }
     */

    public function personalInfo(Request $request){
        $xtoken=$request->header('x-auth-token');
        if(empty($xtoken)){
            return $this->response([],401,"Unauthorized");
        }
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }else{
            return $this->response($result,200);
        }

    }
    /**
     * @api {post} /video/near / 附近
     * @apiVersion 0.0.1
     * @apiName near
     * @apiGroup video
     * @apiDescription 附近
     * @apiParam {string} longitude  当前用户所在经度.
     * @apiParam {string} latitude  当前用户所在纬度.
     * @apiErrorExample Response (example):
     *     返回示例
     *     {
     *       "code": "200"
     *       "message": ""
     *        "result":  true
     *     }
     */

    public function near(Request $request){
        $xtoken=$request->header('x-auth-token');
        if(empty($xtoken)){
            return $this->response([],401,"Unauthorized");
        }
        $result = post_curl($xtoken);
        if($result == false){
            return $this->response([],500,"接口请求错误");
        }
        if($result['code'] != 0){
            return $this->response([],500,"信息返回错误");
        }
        $lng = $request->input('longitude');
        $lat = $request->input('latitude');
        if(empty($lng) && empty($lat)){
            return $this->response([],403,"参数不全");
        }
        $info = array();
        $model = new Video();
        $data =$model->get();
        foreach ($data as $key => $value){
            $info = object_array($value);
            $longitude = $info['longitude'];
            $latitude = $info['latitude'];
            $distance = GetDistance($lat,$latitude,$lng,$longitude,2);
            $info['distance'] = $distance;
            $data[$key][$info] = $info;
        }
        return $this->response($data,200,true);
    }



}
